﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using ReportPrinting;

namespace BSE.Windows.Test
{
    public enum FormType { 
        DEPTDOCFORM, 
        DRUGTYPEFORM,
        DRUGINFORM,
        DRUGOUTFORM,
        HOSPITALREPORTFORM,
        OUTPATIENTREPORTFORM,
        DAYACCOUNTREPORTFORM,
        DRUGACCOUNTREPORTFORM,
        MONTHSUMMARYREPORTFORM,
        MONTHSTATISTICSREPORTFORM,
        TENDAYSREPORTFORM,
        HOSPITALINFORM,
        OUTPATIENTINFORM
    };

    public partial class Form1 : Form
    {
        private List<BaseForm> functionForms = new List<BaseForm>();
        private ReportPrinting.ReportDocument reportDocument1;
        private ReportPrinting.PrintControlToolBar printControlToolBar1;
        
        public Form1()
        {
            InitializeComponent();
            InitializePrint();
            BaseForm f = new DeptDocForm();
            AddFunctionForm(f);
            f = new DrugTypeForm();
            AddFunctionForm(f);
            f = new DrugInForm();
            AddFunctionForm(f);
            f = new PrescriptionForm();
            AddFunctionForm(f);
            f = new HospitalReportForm();
            AddFunctionForm(f);
            f = new SummaryReportForm();
            AddFunctionForm(f);
            f = new AccountForm();
            AddFunctionForm(f);
            f = new TenDaysForm();
            AddFunctionForm(f);
            f = new RegisterZYForm();
            AddFunctionForm(f);
        }

        protected void InitializePrint()
        {
            this.printControlToolBar1 = new ReportPrinting.PrintControlToolBar();
            this.reportDocument1 = new ReportPrinting.ReportDocument();

            // 
            // printControlToolBar1
            // 
            this.printControlToolBar1.Dock = System.Windows.Forms.DockStyle.Top;
            this.printControlToolBar1.Document = this.reportDocument1;
            this.printControlToolBar1.Location = new System.Drawing.Point(0, 0);
            this.printControlToolBar1.Name = "printControlToolBar1";
            this.printControlToolBar1.Size = new System.Drawing.Size(368, 40);
            this.printControlToolBar1.TabIndex = 2;
            this.printControlToolBar1.Printing += new System.EventHandler(this.printControlToolBar1_Printing);
            // 
            // reportDocument1
            // 
            this.reportDocument1.Body = null;
            this.reportDocument1.PageFooter = null;
            this.reportDocument1.PageHeader = null;

            //
            // MainForm
            //
            this.Controls.Add(this.printControlToolBar1);
            this.ResumeLayout(false);
        }

        private void AddFunctionForm(BaseForm f)
        {
            f.TopMost = false;
            f.TopLevel = false;
            f.FormBorderStyle = FormBorderStyle.None;
            f.Parent = panel5;
            f.Visible = false;
            f.Dock = DockStyle.Fill;
            panel5.Controls.Add(f);
            functionForms.Add(f);
        }

        private void ActivateForm(FormType formType)
        {
            BaseForm activeForm = null;
            foreach (BaseForm f in functionForms)
            {
                if (formType == FormType.DEPTDOCFORM)
                {
                    if (f is DeptDocForm)
                        activeForm = f;
                    else
                        f.Visible = false;
                }
                else if (formType == FormType.DRUGTYPEFORM)
                {
                    if (f is DrugTypeForm)
                        activeForm = f;
                    else
                        f.Visible = false;
                }
                else if (formType == FormType.DRUGINFORM)
                {
                    if (f is DrugInForm)
                        activeForm = f;
                    else
                        f.Visible = false;
                }
                else if (formType == FormType.DRUGOUTFORM)
                {
                    if (f is PrescriptionForm)
                        activeForm = f;
                    else
                        f.Visible = false;
                }
                else if (formType == FormType.HOSPITALREPORTFORM)
                {
                    if (f is HospitalReportForm)
                    {
                        HospitalReportForm hf = f as HospitalReportForm;
                        hf.isHospital = 1;
                        activeForm = f;
                    }
                    else
                        f.Visible = false;
                }
                else if (formType == FormType.OUTPATIENTREPORTFORM)
                {
                    if (f is HospitalReportForm)
                    {
                        HospitalReportForm hf = f as HospitalReportForm;
                        hf.isHospital = 0;
                        activeForm = f;
                    }
                    else
                        f.Visible = false;
                }
                else if (formType == FormType.MONTHSUMMARYREPORTFORM)
                {
                    if (f is SummaryReportForm)
                        activeForm = f;
                    else
                        f.Visible = false;
                }
                else if (formType == FormType.DAYACCOUNTREPORTFORM)
                {
                    if (f is AccountForm)
                    {
                        AccountForm hf = f as AccountForm;
                        hf.AccountType = AccountType.DayAccount;
                        activeForm = f;
                    }
                    else
                        f.Visible = false;
                }
                else if (formType == FormType.DRUGACCOUNTREPORTFORM)
                {
                    if (f is AccountForm)
                    {
                        AccountForm hf = f as AccountForm;
                        hf.AccountType = AccountType.DrugAccount;
                        activeForm = f;
                    }
                    else
                        f.Visible = false;
                }
                else if (formType == FormType.TENDAYSREPORTFORM)
                {
                    if (f is TenDaysForm)
                        activeForm = f;
                    else
                        f.Visible = false;
                }
                else if (formType == FormType.HOSPITALINFORM)
                {
                    if (f is RegisterZYForm)
                    {
                        RegisterZYForm rf = f as RegisterZYForm;
                        rf.IsZhuyuan = true;
                        activeForm = f;
                    }
                    else
                        f.Visible = false;
                }
                else if (formType == FormType.OUTPATIENTINFORM)
                {
                    if (f is RegisterZYForm)
                    {
                        RegisterZYForm rf = f as RegisterZYForm;
                        rf.IsZhuyuan = false;
                        activeForm = f;
                    }
                    else
                        f.Visible = false;
                }
            }

            if (activeForm != null)
            {
                activeForm.Visible = true;
                panel5.Text = activeForm.Text;
                activeForm.RefreshForm();
                activeForm.Activate();
            }
        }

        private void m_mnuFile_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        

        private void PanelsCloseClick(object sender, EventArgs e)
        {
            BSE.Windows.Forms.Panel panel = sender as BSE.Windows.Forms.Panel;
            if (panel != null)
            {
                panel.Visible = false;
            }
        }

        private void PanelsVisibleChanged(object sender, EventArgs e)
        {
            BSE.Windows.Forms.Panel panel = sender as BSE.Windows.Forms.Panel;
            if (panel != null)
            {
            }
        }

        

        private void listView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView2.SelectedItems.Count > 0)
            {
                /*
                switch (listView2.SelectedIndices[0])
                {
                    case 0:
                        panel5.Visible = true;
                        panel5.Parent.Controls.SetChildIndex(panel5, 0);
                        break;
                    
                    case 1:
                        panel5.Visible = true;
                        panel5.Parent.Controls.SetChildIndex(panel5, 0);
                        break;

                    case 2:
                        break;
                }
                */
                switch (listView2.SelectedIndices[0])
                {
                    case 0:
                        ActivateForm(FormType.DEPTDOCFORM);
                        break;

                    case 1:
                        ActivateForm(FormType.DRUGTYPEFORM);
                        break;
                }
            }
        }

        private void listView3_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView3.SelectedItems.Count > 0)
            {
                /*
                switch (listView2.SelectedIndices[0])
                {
                    case 0:
                        panel5.Visible = true;
                        panel5.Parent.Controls.SetChildIndex(panel5, 0);
                        break;
                    
                    case 1:
                        panel5.Visible = true;
                        panel5.Parent.Controls.SetChildIndex(panel5, 0);
                        break;

                    case 2:
                        break;
                }
                */
                switch (listView3.SelectedIndices[0])
                {
                    case 0:
                        ActivateForm(FormType.DRUGINFORM);
                        break;

                    case 1:
                        ActivateForm(FormType.OUTPATIENTINFORM);
                        break;

                    case 2:
                        ActivateForm(FormType.HOSPITALINFORM);
                        break;

                    case 3:
                        ActivateForm(FormType.DRUGOUTFORM);
                        break;
                }
            }
        }

        private void listView4_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView4.SelectedItems.Count > 0)
            {
                switch (listView4.SelectedIndices[0])
                {
                    case 0:
                        ActivateForm(FormType.HOSPITALREPORTFORM);
                        break;

                    case 1:
                        ActivateForm(FormType.OUTPATIENTREPORTFORM);
                        break;

                    case 2:
                        ActivateForm(FormType.DAYACCOUNTREPORTFORM);
                        break;

                    case 3:
                        ActivateForm(FormType.DRUGACCOUNTREPORTFORM);
                        break;

                    case 5:
                        ActivateForm(FormType.TENDAYSREPORTFORM);
                        break;

                    case 6:
                        ActivateForm(FormType.MONTHSUMMARYREPORTFORM);
                        break;
                }
            }
        }

        private void printControlToolBar1_Printing(object sender, System.EventArgs e)
        {
            this.reportDocument1.ReportMaker = GetReportMaker();
        }

        private IReportMaker GetReportMaker()
        {
            foreach (BaseForm f in functionForms)
            {
                if (f.Visible)
                {
                    if (f is IReportMaker)
                    {
                        return f as IReportMaker;
                    }
                }
            }
            return null;
        }

        private void listView3_Enter(object sender, EventArgs e)
        {
            listView3_SelectedIndexChanged(listView3, new EventArgs());
        }
    }
}
